<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mobile_rent_module extends CI_Module
{
    private $data;
    private $sid;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('rent.rent_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Orderrent_model');
        $this->load->model('member.User_model');

    }

    public function profile()
    {
        $this->data['cartypo'] = array(
         1 => '豪华轿车',
          2=> '普通商务',
          3=> '豪华商务',
          4=> '中型旅游车',
          5=> '豪华旅游大巴',
        );
        $this->data['seat'] = array(
            1 => '5座',
            2 => '9座',
            3 => '11座',
            4 => '15座',
            5 => '19座',
            6 => '30座以上',
        );

        $id = intval($_GET['id']);

        $this->data['car'] = $this->rent_model->_car_info($id);
        if(!$this->data['car']){
            header('Location : '.site_url('mobile/rent'), true, 302);
            exit;
        }
        $this->data['car']['price_list'] = $this->rent_model->get_price_list(array('car_id'=>$id));

        $this->data['common'] = array(
            array(
                'noback' => false,
                'head'   => $this->data['car']['title'],
                'title'   => $this->data['car']['title'],
                'keywords'   => $this->data['car']['title'] . ',租车,'. SITECITYNAME.'旅游网',
                'description'   => SITECITYNAME.'旅游网-为您提供'.SITECITYNAME.'旅游资讯、'.SITECITYNAME.'旅游攻略、'.SITECITYNAME.'旅游线路，包括'.SITECITYNAME.'热门景区介绍、精彩游记、旅游地图、交通出行、游玩路线、旅游优惠信息、住宿、饮食、购物等旅游信息的综合性旅游门户网站。'
            )
        );
        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'rent_profile', $this->data);
    }

    public function index()
    {
        $title_query = '';
        $where= "sid = ".$this->sid." AND status = 1" ;
        $param = $this->uri->segment(2);
        $param = str_replace(array('list_'), '', $param);
        @list($service_type, $seat, $car_type) = explode('_', $param);
        $service_type = $this->data['service_type'] = intval($service_type);
        $seat     = $this->data['seat']     = intval($seat);
        $car_type    = $this->data['car_type']    = intval($car_type);

        if($service_type)
        {
            switch($service_type)
            {
                case "1" :
                    $where .= " AND airport_service = 1";

                    break;

                case "2" :
                    $where .= " AND short_service = 1";
                    break;

                case "3" :
                    $where .= " AND long_service = 1";
                    break;
            }

            $title_query = $this->rent_model->return_service_type($service_type);
        }

        if($seat)
        {
            $where .= " AND seat = ".$seat;
            $title_query .= $this->rent_model->return_seat_number($seat);
        }

        if($car_type)
        {
            $where .= " AND car_type = ".$car_type;
            $title_query .= $this->rent_model->return_car_type($car_type);
        }
        $page_config['perpage']=10;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']= "/zuche/list_{$service_type}_{$seat}_{$car_type}";//url
        $page_config['nowindex'] = $this->uri->segment(3)>0 ? $this->uri->segment(3):1;//当前页
        $page_config['total'] = $this->rent_model->get_car_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);


        if($_POST['keyword']){
            $keyword = addslashes($_POST['keyword']);
            $where .= " AND ( `title` LIKE '%$keyword%' OR `brand` LIKE '%$keyword%' OR `desc` LIKE '%$keyword%') ";
        }

        $this->data['list'] = $this->rent_model->get_car_list($where,$page_config['perpage'],$page_config['nowindex']);
        $this->data['total_num'] =  $page_config['total'];
        if($this->data['list'])
        {
            foreach($this->data['list'] as $k=>$v)
            {
                $this->data['list'][$k]['price_list'] = $this->rent_model->get_price_list(array('car_id'=>$v['id']));
                $this->data['list'][$k]['car_type_name'] =  $this->rent_model->return_car_type($v['car_type']);
            }

        }

        $this->data['common'] = array(
            array(
                'noback' => false,
                'head'   => SITECITYNAME.'租车',
                'title'   => SITECITYNAME.'租车',
                'keywords'   => SITECITYNAME.'旅游网',
                'description'   => SITECITYNAME.'旅游网-为您提供'.SITECITYNAME.'旅游资讯、'.SITECITYNAME.'旅游攻略、'.SITECITYNAME.'旅游线路，包括'.SITECITYNAME.'热门景区介绍、精彩游记、旅游地图、交通出行、游玩路线、旅游优惠信息、住宿、饮食、购物等旅游信息的综合性旅游门户网站。'
            )
        );
        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'rent_list', $this->data);
    }

    public function order(){
        $this->data['cartypo'] = array(
            1 => '豪华轿车',
            2=> '普通商务',
            3=> '豪华商务',
            4=> '中型旅游车',
            5=> '豪华旅游大巴',
        );
        $this->data['seat'] = array(
            1 => '5座',
            2 => '9座',
            3 => '11座',
            4 => '15座',
            5 => '19座',
            6 => '30座以上',
        );
        $id = $this->input->get_post('id');
        @$user_id = $this->session->userdata('user_id');
        if($user_id<1){
            redirect('mobile/member/login?referer='.urlencode('mobile/rent/order?id='.$id.'&preview='.$_GET['preview'].'&touchtpl='.$_GET['touchtpl']));
        }

        $d0 = date('Y-m-d');
        $d1 = date('Y-m-d', strtotime('+3 month'));
        $_time = range(strtotime($d0), strtotime($d1), 24*60*60);
        $this->data['_time'] = array_map(create_function('$v', 'return date("Y-m-d", $v);'), $_time);


        $this->data['price_info'] = $this->rent_model->_car_price_info($id);
        $this->data['price_info']['service_type_name'] = $this->rent_model->return_service_type($this->data['price_info']['cate_attr']);

        $this->data['car'] = $this->rent_model->_car_info($id);
        $this->data['car']['price_list'] = $this->rent_model->get_price_list(array('car_id'=>$id));

        $this->data['car']['seat_number'] = $this->rent_model->return_seat_number($this->data['car']['seat']);
        $this->data['car']['car_type_name'] = $this->rent_model->return_car_type($this->data['car']['car_type']);
        $this->data['price_id'] = $id ;
        $this->data['form_url'] = site_url('rent/index/order') ;
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            @$price_id = intval($_POST['id']);
            @$contact_name = trim($_POST['contact_name']);
            @$contact_phone = trim($_POST['contact_phone']);
            @$go_date =  $_POST['go_date'];
            $days = abs(intval($_POST['days']));
            $carnumber = abs(intval($_POST['carnumber']));

            if(
                empty($go_date) ||empty($price_id) ||  empty($contact_name) || empty($contact_phone) || empty($days) || empty($carnumber)
            ){
                $this->error('数据异常，订单生成失败！', '', TRUE);
            }



            //订单信息
            $order_data = array(
                'sid' =>$GLOBALS['sid'],
                'order_sn'      => $this->Orderinfo_model->set_order_sn(),
                'order_type'    => 5,//门票订单
                'cat_type'      => 2,//网上订单
                'order_account' => $this->data['car']['price_list'][0]['ws_price']*$days*$carnumber,
                'order_status'  => 2, //门票无需审核
                'order_note'  => @trim($_POST['order_note']), //门票无需审核
                'pay_status'    => 1,//未支付
                'add_time'      => date('Y-m-d H:i:s',time()),
                'user_id'       => $user_id,//用户ID
                'admin_user_id' => intval( get_cookie('from_card')),//管理员ID
            );
            $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

            if($order_id){
                //联系人信息
                $contact_data = array(
                    'order_id'  => $order_id,
                    'name'      => $contact_name,
                    'phone_num' => $contact_phone,
                );
                $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);

                //订单租车信息
                $row = array(
                    'sid' =>$this->sid,
                    'order_id'  => $order_id,
                    'price_id' => $price_id,
                    'go_date' => $go_date,
                    'car_id' =>  $this->data['car']['id'],
                    'price'     => $this->data['car']['price_list'][0]['ws_price'],
                );
                $add_return_status = $this->Orderrent_model->add_row($row);

                if($contact_return_status&&$add_return_status)
                {
                    $submit_template = $GLOBALS['config']['rent_order_template_submit'];
                    $target = array(
                        '{$order_sn}',
                        '{$pay_money}',
                        '{$date}',
                        '{$brand}',
                    );
                    $replace = array(
                        $order_data['order_sn'],
                        $order_data['order_account'],
                        $row['go_date'],
                        $this->data['car']['brand']
                    );

                    $content = str_replace($target,$replace,$submit_template);
                    $this->User_model->_send_sms($contact_data['phone_num'],$content);

                    $this->success('提交订单成功!',site_url('mobile/order/rent_order_profile/?id='.$order_id),true);
                }
                else
                {
                    $this->error('订单生成失败!', '',true);
                }
            }
            else
            {
                $this->error('订单生成失败!', '',true);
            }
        }
        $this->data['common'] = array(
            array(
                'noback' => false,
                'head'   => SITECITYNAME.'租车',
                'title'   => SITECITYNAME.'租车',
                'keywords'   => SITECITYNAME.'旅游网',
                'description'   => SITECITYNAME.'旅游网-为您提供'.SITECITYNAME.'旅游资讯、'.SITECITYNAME.'旅游攻略、'.SITECITYNAME.'旅游线路，包括'.SITECITYNAME.'热门景区介绍、精彩游记、旅游地图、交通出行、游玩路线、旅游优惠信息、住宿、饮食、购物等旅游信息的综合性旅游门户网站。'
            )
        );
        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'rent_order', $this->data);
    }
}